package bl.controller;

import java.util.ArrayList;
import java.util.Iterator;

import po.CatalogPO;

import database.factory.CatalogDataFactory;
import database.factory.CatalogDataFactoryImpl;
import database.service.CatalogDataService;

import vo.CatalogVO;
import bl.domain.CatalogInquiry;
import bl.service.CatalogInquiryBLService;

public class CatalogInquiryController implements CatalogInquiryBLService {
	
	private CatalogInquiry catalogInquiry;
	
	public CatalogInquiryController() {
		catalogInquiry = new CatalogInquiry();
	}

	private void init() {
		CatalogDataFactory catalogDataFactory = new CatalogDataFactoryImpl();
		CatalogDataService catalogDataService = catalogDataFactory.getCatalogDataService();
		catalogDataService.load();
		catalogInquiry.setCatalogItr(catalogDataService.getIterator());
	}

	@Override
	public ArrayList<CatalogVO> getCatalogList() {
		init();
		Iterator<CatalogPO> catalogItr = catalogInquiry.getCatalogItr();
		ArrayList<CatalogVO> catalogVOs = new ArrayList<CatalogVO>();
		while (catalogItr.hasNext()) {
			CatalogPO cpo = catalogItr.next();
			if (cpo.getID() > 0) {
				catalogVOs.add(new CatalogVO(cpo));
			}
		}
		return catalogVOs;
	}

	@Override
	public String getName(int catalogID) {
		init();
		while (catalogInquiry.getCatalogItr().hasNext()) {
			CatalogPO catalog = catalogInquiry.getCatalogItr().next();
			int id = catalog.getID();
			if (id == catalogID) {
				return catalog.getName();
			}
		}
		return "";
	}

	@Override
	public int getCatalogID(String name) {
		init();
		while (catalogInquiry.getCatalogItr().hasNext()) {
			CatalogPO catalog = catalogInquiry.getCatalogItr().next();
			String catalogName = catalog.getName();
			if (name.equals(catalogName)) {
				return catalog.getID();
			}
		}
		return 0;
	}

}
